﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D ;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Lyric
{
    public partial class Form1 : Form
    {
        Point offset;
        Dictionary<double, string> lyrics = new Dictionary<double, string>();
        double now=0;

        public Form1()
        {
            InitializeComponent();
        }

        public Form1(string filePath):this()
        {
            //if(!File.Exists(filePath))
            FileStream readFile = new FileStream(filePath, FileMode.Open);
            StreamReader read = new StreamReader(readFile,Encoding.UTF8 );
            string line = read.ReadLine();
            while (line != null)
            {
                dealString(line);
                line=read.ReadLine();
            }
        }
        private void dealString(string s)
        {
            string time,minute,second,ms;
            string lyric;
            time=s.Substring(1,s.IndexOf(']',1)-1);
            minute=time.Substring(0,time.IndexOf(':'));
            second=time.Substring(time.IndexOf(':')+1,2);
            ms=time.Substring(time.IndexOf('.')+1);

            lyric=s.Substring(s.IndexOf(']')+1).Trim();
            if(lyric!=null&&lyric!="")
            {
                lyrics.Add( Convert.ToInt32(minute)*60 + Convert.ToInt32(second) + Convert.ToDouble(ms)/100,lyric);
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Brush b=new LinearGradientBrush(new Point(50,0),new Point(50,60),Color.Green,Color.Blue );
            try
            {
                string showLyric = lyrics[now];
                g.FillRectangle(Brushes.White, new Rectangle(0, 0, 600, 80));
                g.DrawString(showLyric, new Font("微软雅黑", 16, FontStyle.Bold), b, new Point(20, 20));
                g.Dispose();
            }
            catch
            {
                
            }
            finally
            {
                 now=Math.Round(now+0.01, 2);
            }
            
            
        }
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left != e.Button) return;

            Point cur = this.PointToScreen(e.Location);
            offset = new Point(cur.X - this.Left, cur.Y - this.Top);
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left != e.Button) return;

            Point cur = MousePosition;
            this.Location = new Point(cur.X - offset.X, cur.Y - offset.Y);
        }
    }
}
